<?php 
namespace app\Admin\controller;
use think\Controller;
use think\Db;
use app\Admin\model\Cate;
use think\Request;
class Category extends Base
{
	/*分类展示*/
	public function cate_list(){
		$result = Db('category')->order('cat_id desc')->paginate();
		$this->assign('list',$result);
		return $this->fetch();
	}

	/*分类添加*/
	public function cate_add(){
		$cate = new Cate;
		if(request()->isPost()){
			$data['cat_name'] = input('post.cat_name');
			$data['parent_id'] = input('post.parent_id');
			$data['is_show'] = input('post.is_show');
			$data['is_nav'] = input('post.is_nav');
			$data['cat_desc'] = input('post.cat_desc');
			$data['path'] = input('post.path');
			$re = $cate->addData($data);
			$this->redirect('Category/cate_list');
		}else{
			$cate = $cate->getPathList("cat_id");
			$this->assign('cate',$cate);
			return $this->fetch();
		}	
	}

	/*分类删除*/
	public function cate_del(){
		$cat_id=input("get.id");
        //查询是否有子集
        $res=Db("Category")->where('parent_id',$cat_id)->select();
        if (!empty($res)){
            $arr=[
				'code' => 0,
				'msg' => '有子集,无法删除'
			];
			
        }else{
        	$result=Db("Category")->where('cat_id',$cat_id)->delete();
        	$arr=[
				'code' => 0,
				'msg' => '删除成功'
			];
        }
        echo json_encode($arr);      
	}


	/*分类修改*/
	public function cate_save(){
		 $cat_name = input('get.cat_name');
		 $cat_desc = input('get.cat_desc');
		 $cat_id = input('get.id');
		 $result = Db('category')->where('cat_id',$cat_id)->update(['cat_name' => $cat_name,'cat_desc'=>$cat_desc]);
		 echo json_encode($result);
	}

	/*分类导航修改*/
	public function cate_update(){
		$id = input('get.id');
		$is_nav = input('get.is_nav');
		$result = Db('category')->where('cat_id',$id)->update(['is_nav' => $is_nav]);	
		echo json_encode($result);	
		
	}

	/*分类显示修改*/
	public function cate_upshow(){
		$id = input('get.id');
		$is_show = input('get.is_show');
		$result = Db('category')->where('cat_id',$id)->update(['is_show' => $is_show]);	
		echo json_encode($result);	

	}

}